package com.whately.core.base.jwt.util;

import com.whately.core.base.constant.ConstantProject;
import com.whately.core.base.constant.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;

/**
 * jwt session
 */
@Component
@Slf4j
public class JwtSessionUtil {
    //3600, 即一小时
    @Value("${spring.session.timeout:3600}")
    private int maxInactiveIntervalInSeconds;

    //10天
    private int defaultHours = 24*10;


    public String addSession(HttpServletResponse response, LoginUser loginUser) {
        return addSession(response,loginUser,defaultHours);

    }


    public String addSession(HttpServletResponse response, LoginUser loginUser,int hours) {

        String token = JWTUtil.geneJsonWebToken(loginUser, maxInactiveIntervalInSeconds * hours);//24小时 * 10 = 10天

        response.setHeader(ConstantProject.response_header_token_name, token);
        return token;

    }

}

